package utils;

import java.io.File;  
import java.io.IOException;
import java.io.FileInputStream;  
import java.io.InputStream;  

public class JpegUtil
{

private  Long iWidth = 0L;
private  Long iHeight = 0L;
private  Boolean fileexist = false;
private  Boolean isjpeg = false;

public  String HexString( byte[] b) { 
  String result="";
     for (int i = 0; i < b.length; i++) { 
       String hex = Integer.toHexString(b[i] & 0xFF); 
       if (hex.length() == 1) { 
         hex = '0' + hex; 
       } 
       
       result= result + hex.toUpperCase();  
       
     } 
     return result;
 }


  public  void frameParse(String filename) throws IOException
  {
            File f = new File(filename);    

            if (f.exists())    
            {    
               fileexist = true;
               

               byte[] flags = new byte[2];
               InputStream in = new FileInputStream(filename);
               in.read(flags);
                              
               if (HexString(flags).equals("FFD8")){
               	isjpeg = true;
               	}
               
               if (isjpeg == false) return;	
               
               
               byte[] bdata = new byte[1];
               String tempframe1 = "";
               boolean skipframe = false;
               for (int i = 0; i < 1024; i++) { 
                   in.read(bdata);	
                   
                   if (HexString(bdata).equals("C0")) 
                   	  if (tempframe1.equals("FF")){
                   	    skipframe = true;
                   	    break;
                   	  }
                  tempframe1 = HexString(bdata); 	

               }

               if (skipframe == false) return;
               in.skip(3);
               in.read(flags);
               iHeight = Long.valueOf(HexString(flags),16);               
               in.read(flags);
               iWidth = Long.valueOf(HexString(flags),16);               
                             

            }  

  	
  	
  	}

  public  Long getWidth()
  {
  	 return iWidth;
  }
  
  public  Long getHeight()
  {
  	 return iHeight;
  }
  
  public  Boolean getFileexist()
  {
  	 return fileexist;
  }
  
  public  Boolean getIsjpeg()
  {
  	 return isjpeg;
  }
  
	public static void main(String[] args) throws IOException
	{

            String filename = args[0];
            JpegUtil jpeg = new JpegUtil();
            jpeg.frameParse(filename); 
            if (jpeg.getFileexist() == false){
              System.out.println("文件不存在");
              return;
            }
            
            if (jpeg.getIsjpeg()){
              System.out.println("是Jpeg文件");
              System.out.println("宽度："+jpeg.getWidth().toString());   
              System.out.println("高度："+jpeg.getHeight().toString());   
            }else{
              System.out.println("不是Jpeg文件");
            }
           
  }

}

